package com.item.Controller;

import com.item.POJO.BaseDict;
import com.item.POJO.Customer;
import com.item.POJO.User;
import com.item.Service.BaseDictService;
import com.item.Service.CustomerService;
import com.item.common.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private BaseDictService baseDictService;
    //客户来源
    @Value("${customer.from.type}")
    private String FROM_TYPE;
    //客户所属行业
    @Value("${customer.industry.type}")
    private String INDUSTRY_TYPE;
    //客户级别
    @Value("${customer.level.type}")
    private String LEVEL_TYPE;

    @RequestMapping(value = "/customer/list.action")
    public String list(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int rows,
                       String custName,String custSource,String custIndustry,String custLevel, Model model)
    {
        Page<Customer> customers = customerService.selectCustomerList(page, rows, custName, custSource, custIndustry, custLevel);
        model.addAttribute("page",customers);
        //客户来源
        List<BaseDict> fromType = baseDictService.selectBaseDictByTypeCode(FROM_TYPE);
        //客户所属行业
        List<BaseDict> industryType = baseDictService.selectBaseDictByTypeCode(INDUSTRY_TYPE);
        //客户级别
        List<BaseDict> levelType = baseDictService.selectBaseDictByTypeCode(LEVEL_TYPE);
        //添加参数
        model.addAttribute("fromType",fromType);
        model.addAttribute("industryType",industryType);
        model.addAttribute("levelType",levelType);
        model.addAttribute("custName",custName);
        model.addAttribute("custSource",custSource);
        model.addAttribute("custIndustry",custIndustry);
        model.addAttribute("custLevel",custLevel);
        return "customer";
    }

    /**
     *创建用户
     */
    @RequestMapping("/customer/create.action")
    @ResponseBody
    public String customerCreate(Customer customer, HttpSession session){

        User user = (User) session.getAttribute("USER_SESSION");
        customer.setCust_create_id(user.getUser_id());
        Date date = new Date();
        //得到一个Timestamp格式的时间，存入mysql中的事件格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        customer.setCust_createtime(timeStamp);
        int rows = customerService.createCustomer(customer);
        if (rows > 0){
            return "OK";
        }
        return "FAIL";
    }

    /**
     * 通过id获取客户信息
     */
    @RequestMapping("/customer/getCustomerById.action")
    @ResponseBody
    public Customer getCustomerById(int id){
        return  customerService.getCustomerById(id);
    }

    /**
     *更新客户
     */
    @RequestMapping("/customer/update.action")
    @ResponseBody
    public String customerUpdate(Customer customer){
        int rows = customerService.customerUpdate(customer);
        if (rows > 0){
            return "OK";
        }else {
            return "FAIL";
        }
    }

    /**
     * 删除用户
     */
    @RequestMapping("/customer/delete.action")
    @ResponseBody
    public String customerDelete(int id){
        int rows = customerService.deleteCustomer(id);
        if(rows > 0){
            return "OK";
        }
        return "FAIL";
    }
}
